package a66weding.com.jiehuntong.common.utils;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.annotation.StringRes;
import android.text.format.Formatter;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.util.DensityUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import a66weding.com.jiehuntong.R;

import static android.widget.Toast.makeText;

/**
 * 项目工具类
 *
 */
public final class CUtils {

    private static Application mApplication;
    private static Gson mGson;
    private static Toast mToast;


    private CUtils() {
    }

    /**
     * 初始化一个 Application 供全局使用
     *
     * @param application Application 对象
     */
    public static void init(Application application) {
        mApplication = application;
        mGson = new Gson();
    }

    /**
     * @return 返回 Application 对象
     */
    public static Application getApplication() {
        return mApplication;
    }

    /**
     * @return 返回 Gson 对象
     */
    public static Gson getGson() {
        return mGson;
    }


    /**
     * 根据 TAG 名称获取对应的 drawable 资源 ID
     *
     * @param tagName TAG 名称
     * @return 如果存在返回对应的 drawable 资源 ID, 否则返回 null
     */
    public static Integer getTagResId(String tagName) {

        return null;
    }



    /**
     * 获取应用版本名称
     *
     * @return 应用版本号字符串, 如果不存在返回 null
     */
    public static String getAppVersionName() {
        PackageManager manager = getApplication().getPackageManager();
        try {
            return manager.getPackageInfo(getApplication().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }


    //版本号
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pi;
    }
    /**
     * 显示Toast提示
     */
    public static void showToast(@StringRes int resId) {
        makeText(getApplication(), resId, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示Toast提示在屏幕底部(系统默认位置)
     */
    public static void showToast(CharSequence msg) {
        cancelToast();
        mToast=Toast.makeText(getApplication(),msg,Toast.LENGTH_SHORT);
        mToast.show();
    }

    /**
     *显示提醒弹窗在屏幕中间
     * @param msg
     */
    public static void showToastInCenter(CharSequence msg) {
        cancelToast();
        mToast=  Toast.makeText(getApplication(),msg,Toast.LENGTH_SHORT);
        mToast.setGravity(Gravity.CENTER,0,0);
        mToast.show();
    }

    /**
     * 显示Toast提示
     */
    public static void showDataErrToast(Context context) {
        if (mToast==null){
            mToast=new Toast(context);
        }
        View errVIew = View.inflate(context, R.layout.dialog_dataerror, null);
        mToast.setGravity(Gravity.CENTER, 0, DensityUtil.dip2px(100));
        mToast.setDuration(Toast.LENGTH_LONG);
        mToast.setView(errVIew);
        mToast.show();
    }

    /**
     *toast取消
     */
    public static void cancelToast(){
        if(mToast != null){
            mToast.cancel();
            mToast = null;
        }
    }
    /**
     * 返回当前时间
     */
    public static String getCurrentTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return format.format(new Date());
    }

    /**
     * 返回容易辨认的字节数
     */
    public static String formatSize(long size) {
        if (size <= 0)
            return "0B";
        return Formatter.formatFileSize(mApplication, size);
    }



}
